package com.flute.framework.search;

import com.flute.framework.FluteMonitor;
import com.flute.framework.exception.SearchException;
import com.flute.framework.monitor.EventLogger;
import com.flute.framework.monitor.MonitedValue;
import com.flute.framework.search.cache.CachedSearchResult;
import com.flute.framework.search.cache.IResultCache;
import com.flute.framework.search.result.SearchResult;

public class CachedSearcher implements ISearcher {
	private ISearcher searcher;
	private IResultCache cache;

	public String search(SearchRequest request) throws SearchException {
		MonitedValue mv = fromSearchRequest(request);

		long start = System.currentTimeMillis();
		boolean cached = false;

		String result = "";

		if (cache != null) {
			String key = request.toString();
			CachedSearchResult cachedResult = (CachedSearchResult) cache
					.getCached(key);

			if (cachedResult != null) {
				result = cachedResult.getSearchResult();
				cached = true;
			} else {
				result = searcher.search(request);
				CachedSearchResult csr = new CachedSearchResult();
				csr.setSearchResult(result);
				csr.setUpdateDate(System.currentTimeMillis());
				cache.cache(key, csr);
			}
		} else {
			result = searcher.search(request);
		}

		mv.addFieldValue("花费时间", String.valueOf(System.currentTimeMillis()
				- start));
		mv.addFieldValue("是否缓存", String.valueOf(cached));

		EventLogger.info(mv, FluteMonitor.getSearcherKey());

		return result;
	}

	private MonitedValue fromSearchRequest(SearchRequest request) {
		MonitedValue mv = new MonitedValue();
		mv.addFieldValue("搜索名", request.getSearcherName());
		mv.addFieldValue("关键字", request.getKeyword());
		mv.addFieldValue("页码", String.valueOf(request.getPageNumber()));
		mv.addFieldValue("客户端", request.getClient());
		return mv;
	}

	@Override
	public String getSearcherName() {
		return searcher.getSearcherName();
	}

	@Override
	public ISearcherProvider getSearcherProvider() {
		return searcher.getSearcherProvider();
	}

	@Override
	public void setSearcherProvider(ISearcherProvider searcherProvider) {
		this.searcher.setSearcherProvider(searcherProvider);
	}

	@Override
	public SearchResult nakeSearch(SearchRequest request)
			throws SearchException {
		MonitedValue mv = fromSearchRequest(request);

		long start = System.currentTimeMillis();
		boolean cached = false;

		SearchResult result = new SearchResult();

		if (cache != null) {
			String key = request.toString();
			CachedSearchResult cachedResult = (CachedSearchResult) cache
					.getCached(key);

			if (cachedResult != null) {
				result = cachedResult.getNakeResult();
				cached = true;
			} else {
				result = searcher.nakeSearch(request);
				CachedSearchResult csr = new CachedSearchResult();
				csr.setNakeResult(result);
				csr.setUpdateDate(System.currentTimeMillis());
				cache.cache(key, csr);
			}
		} else {
			result = searcher.nakeSearch(request);
		}

		mv.addFieldValue("花费时间", String.valueOf(System.currentTimeMillis()
				- start));
		mv.addFieldValue("是否缓存", String.valueOf(cached));

		EventLogger.info(mv, FluteMonitor.getSearcherKey());

		return result;
	}
}
